package com.hankol.newretail

import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.extension.checkNotNull
import com.baselibrary.extension.checkValue
import com.baselibrary.manager.LoadingManager
import com.baselibrary.manager.SensorsDataManager
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.utils.CommonUtil
import com.baselibrary.utils.OptionUtils
import com.baselibrary.activity.BaseActivity
import com.hankol.newretail.adapter.LogisticsAdapter
import com.hankol.newretail.adapter.order.MyOrderListAdapter
import com.hankol.newretail.bean.LogisticeInfoBean
import com.hankol.newretail.databinding.ActivityLogisticsBinding
import com.hankol.newretail.viewmodel.OrderAboutViewModel

/**物流详情
 * @author: merbng
 * @time: 2022/8/22 10:43
 * @description:
 */
@Route(path = ActivityRouter.NEW_RETAIL.MY_LOGISTICS)
class LogisticsActivity : BaseActivity() {
    private lateinit var viewModel: OrderAboutViewModel
    private val mLogisticsAdapter: LogisticsAdapter = LogisticsAdapter()
    private val mOrderCoverListAdapter: MyOrderListAdapter.OrderCoverListAdapter =
        MyOrderListAdapter.OrderCoverListAdapter()
    private lateinit var dataBinding: ActivityLogisticsBinding
    var poId = ""
    var kuaiDiNum = ""
    override fun getLayoutId(): Int {
        return R.layout.activity_logistics
    }

    override fun init() {
        dataBinding = getDataBinding<ActivityLogisticsBinding>()
        viewModel = getActivityViewModelProvider(this).get(OrderAboutViewModel::class.java)

        if (ARouterUtils.parseParams(intent.extras!!, Constants.KEY_LOGISTICS_ORDER_NUM)[0].checkNotNull()) {
            poId = ARouterUtils.parseParams(intent.extras!!, Constants.KEY_LOGISTICS_ORDER_NUM)[0]
        }

        dataBinding.rvLogisticsCover.adapter = mOrderCoverListAdapter
        dataBinding.rvLogisticsList.adapter = mLogisticsAdapter
        loadData()
        initListener()
        initObserver()
        SensorsDataManager.instance.logisticsDetails()
    }

    private fun initObserver() {
        //顶部 订单的商品
        viewModel.orderLoadDetailLD.observe(this, Observer {
            if (it.checkNotNull()) {
                mOrderCoverListAdapter.setNewData(it.shopDto[0].mallOrderSkuDtoList)
            } else {
                LoadingManager.hideProgress(this)
                finish()
            }
        })
        //快递信息
        viewModel.logisticsInfoLD.observe(this, Observer {
            showLogisticsInfo(it)
        })
    }

    //物流信息
    private fun showLogisticsInfo(bean: LogisticeInfoBean) {
        if (bean.wuliu.checkNotNull()) {
            if (bean.wuliu[0].data.checkNotNull()) {
                val datalist = bean.wuliu[0].data
                dataBinding.tvLogisticsCompanyName.text = "物流公司：${bean.kuaiDiInfos[0].kuaiDiCom}"
                kuaiDiNum = bean.kuaiDiInfos[0].kuaiDiNum
                dataBinding.tvLogisticsOddNumbers.text = "运单编号：${bean.kuaiDiInfos[0].kuaiDiNum}"
                dataBinding.tvLogisticsState.setText(OptionUtils.kuaidiStatus(datalist[0].status.checkValue()))
                mLogisticsAdapter.setNewData(datalist)
            }
        }
    }

    fun loadData() {
        viewModel.requestOrderDetail(poId)
        viewModel.reqLogisticsInfo(poId)
    }

    fun initListener() {
        dataBinding.ivBack.setOnClickListener { finish() }
        dataBinding.tvLogisticsCopyNumbers.setOnClickListener {
            if (kuaiDiNum.checkNotNull()) {
                CommonUtil.contentClipboard(this, kuaiDiNum, "复制成功")
            }
        }
    }
}